let express = require('express');
let router = express.Router();

const importService = require('../service/importService');

// 设置JSON解析的最大请求体大小
router.use(express.json({ limit: '50mb' }));                        // 支持JSON格式的数据
router.use(express.urlencoded({ limit: '50mb', extended: true }));  // 支持表单编码数据

const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');

// 此路由需要登录后才能访问
router.use(checkJWTMiddleWare);

/*
 * 导入学生基本信息接口
 */
router.post('/importStudentsInfo', async function (req, res, next) {
    let data = req.body.data;
    if (!(Array.isArray(data)) || data.length == 0) {
        res.status(400).json({ success: false, msg: "参数错误，导入失败" });
        return ;
    }
    let result = await importService.importStudentsInfo(req.jwtResult.data.uid, req.jwtResult.data.userType, data);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 导入普查访谈记录接口
 */
router.post('/importCensusInterviewRecord', async function (req, res, next) {
    let data = req.body.data;
    if (!(Array.isArray(data)) || data.length == 0) {
        res.status(400).json({ success: false, msg: "参数错误，导入失败" });
        return ;
    }
    let result = await importService.importCensusInterviewRecord(req.jwtResult.data.uid, req.jwtResult.data.userType, data);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 导入测评记录接口
 */
router.post('/importAssessRecords', async function (req, res, next) {
    let data = req.body.data;
    if (!(Array.isArray(data)) || data.length == 0) {
        res.status(400).json({ success: false, msg: "参数错误，导入失败" });
        return ;
    }
    let result = await importService.importAssessRecords(req.jwtResult.data.uid, req.jwtResult.data.userType, data);
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;